from sam.util.httpUtil import http_post_json, http_post

NotifyUrl = "https://oapi.dingtalk.com/robot/send?" \
            "access_token=f95a0af60d10d19b0ab05a1f4e0216af2f003b36c00e9439a71fc04078f9b43a"


class DingTalkNotifyUtil(object):
    def __init__(self, notify_url: str = NotifyUrl):
        self.notify_url = notify_url
        self.msg_type = "markdown"

    def notify(self, title: str, text: str):
        data = {
            "msgtype": self.msg_type,
            "markdown": {
                "title": title,
                # 这里的 ### 意思是 三级标题的意思
                "text": f"### {text} \n"
            }
        }
        http_post_json(url=self.notify_url, data=data)


def notify_by_ding_talk(title: str, text: str):
    process = DingTalkNotifyUtil()
    # process.notify("标题", "python 发送 钉钉通知消息 正文内容")
    process.notify(title, text)


def ding_talk_notify_demo():
    process = DingTalkNotifyUtil()
    process.notify("标题", "python 发送 钉钉通知消息 正文内容")


WX_NOTIFY_URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=d3ab95c5-35be-4266-b86e-973f3c3d0303"


class WorkWeChartNotifyUtil(object):
    class Text(object):
        def __init__(self, notify_url: str = WX_NOTIFY_URL):
            super().__init__()
            self.notify_url = notify_url

        def notify(self, content: str, mentioned_mobile_list: list = None, notify_all: bool = True):
            mentioned_mobile_list = mentioned_mobile_list if mentioned_mobile_list else []
            if notify_all:
                mentioned_mobile_list.append("@all")
            notify_body = {
                "msgtype": "text",
                "text": {
                    "content": content,
                    "mentioned_mobile_list": mentioned_mobile_list
                }
            }

            http_post(url=self.notify_url, data=notify_body, optional="json")

        def quick_notify(self, content: str = "企业微信通知测试"):
            self.notify(content=content, notify_all=False, mentioned_mobile_list=["17090130391"])


def work_we_chart_notify(msg: str):
    process = WorkWeChartNotifyUtil.Text()
    process.notify(content=msg)


def work_we_chart_notify_demo():
    process = WorkWeChartNotifyUtil.Text()
    process.notify(content="企业微信通知测试")


if __name__ == "__main__":
    # work_we_chart_notify_demo()
    work_we_chart_notify("aaa")
